package com.dz.risk.aspect;

import com.dz.risk.exception.VerifyException;
import com.dz.risk.pojo.domain.ReportFormsEntity;
import com.dz.risk.service.ReportFormsService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 动态表体系切面处理类
 *
 * @author Doke
 * @date 2021/7/27 16:49
 */
@Component
@Aspect
public class DynamicReportFormsAop {

    private final ReportFormsService reportFormsService;

    @Autowired
    public DynamicReportFormsAop(ReportFormsService reportFormsService) {
        this.reportFormsService = reportFormsService;
    }

    /**
     * 验证动态表导入基本信息值是否传值
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.dz.risk.service.impl.DynamicReportFormsServiceImpl.dynamicImportExcel(..))")
    public Object checkDynamicImportExcel(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        MultipartFile file = (MultipartFile) args[0];
        ReportFormsEntity reportFormsEntity = (ReportFormsEntity) args[1];

        //验证必填参数
        checkRequiredParameters(reportFormsEntity);

        //验证版本是否重叠
        checkVersionOverlapping(reportFormsEntity);

        //验证文件
        checkFile(file);

        return point.proceed();
    }

    /**
     * 验证文件是否为空
     *
     * @param file 文件
     */
    private void checkFile(MultipartFile file) {
        if (Objects.isNull(file)) {
            throw new VerifyException("报表文件未上传");
        }
    }

    /**
     * 验证必填参数是否已传
     *
     * @param reportFormsEntity 报表基本信息
     */
    private void checkRequiredParameters(ReportFormsEntity reportFormsEntity) {
        if (StringUtils.isEmpty(reportFormsEntity.getReportFormsName())
                || StringUtils.isEmpty(reportFormsEntity.getReportFormsSerialNumber())
                || StringUtils.isEmpty(reportFormsEntity.getSuperiorReportFormsSerialNumber())
                || Objects.isNull(reportFormsEntity.getBeginDate())
                || Objects.isNull(reportFormsEntity.getEndDate())) {
            throw new VerifyException("报表编号、报表名称、上级报表编号、是否作为原始数据以及报表开始结束时间为必填参数");
        }
    }

    /**
     * 验证版本是否重叠
     *
     * @param reportFormsEntity 报表基本信息实体
     */
    private void checkVersionOverlapping(ReportFormsEntity reportFormsEntity) {
        //根据报表编号以及版本号查询报表基本信息
        List<ReportFormsEntity> dataBaseReportForms = reportFormsService.selectByReportFormsNameAndVersion(reportFormsEntity.getReportFormsSerialNumber(), reportFormsEntity.getBeginDate(), reportFormsEntity.getEndDate());

        if (!CollectionUtils.isEmpty(dataBaseReportForms)) {
            throw new VerifyException("该报表已存在包含此开始时间或结束时间区间的版本");
        }

    }
}
